package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.gusha.dao.ConfigSalesmanDao;
import com.gusha.model.ConfigSalesman;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:ConfigSalesmanService服务类
 */

@Service
public class ConfigSalesmanService {

    @Autowired
    private ConfigSalesmanDao configSalesmanDao;

    public Page<ConfigSalesman> findAll(Pageable pageable, ConfigSalesman configSalesman) {
        @SuppressWarnings("serial")
        Page<ConfigSalesman> result = configSalesmanDao.findAll(new Specification<ConfigSalesman>() {
            @Override
            public Predicate toPredicate(Root<ConfigSalesman> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (configSalesman.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),configSalesman.getId()));
                }
                if (StringUtils.isNotBlank(configSalesman.getTitle())) {
                    list.add(cb.equal(root.get("title").as(String.class),configSalesman.getTitle()));
                }
                if (configSalesman.getAddCustomer()!=null) {
                    list.add(cb.equal(root.get("addCustomer").as(BigDecimal.class),configSalesman.getAddCustomer()));
                }
                if (configSalesman.getAchievement()!=null) {
                    list.add(cb.equal(root.get("achievement").as(BigDecimal.class),configSalesman.getAchievement()));
                }
                if (configSalesman.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),configSalesman.getCreateTime()));
                }
                if (configSalesman.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),configSalesman.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(configSalesman.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),configSalesman.getCreator()));
                }
                if (StringUtils.isNotBlank(configSalesman.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),configSalesman.getUpdator()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
